package com.surpassun.emall.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

public class CacheUtil {
	
	private static Map<String, Object> cache = new HashMap<String, Object>();
	private static ServletContext context;
	
	public static Map<String, Object> init(HttpServletRequest request) {
		setContext(request.getServletContext());
		getContext().setAttribute(Constants.ATTR_APPLICATION_CACHE, cache);
		return cache;
	}

	public static Object getObject(String key) {
		return getApplicationCache().get(key);
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> getApplicationCache() {
		return (Map<String, Object>) getContext().getAttribute(Constants.ATTR_APPLICATION_CACHE);
	}
	
	public static void addToCache(String key, Object value) {
		getApplicationCache().put(key, value);
	}

	public static Map<String, Object> getCache() {
		return cache;
	}

	public static void setCache(Map<String, Object> cache) {
		CacheUtil.cache = cache;
	}

	public static ServletContext getContext() {
		return context;
	}

	public static void setContext(ServletContext context) {
		CacheUtil.context = context;
	}
}
